package com.yjd.api.home;

import com.yjd.api.util.PicsResourceUtils;
import com.yjd.comm.base.control.MobileBaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.doctor.service.IDoctorService;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.project.model.ProjectModel;
import com.yjd.comm.project.service.IProjectService;
import com.yjd.comm.resource.model.ResourceModel;
import com.yjd.comm.resource.service.IResourceService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ResultMapBuilder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <b>description</b>：标的相关 <br>
 * <b>time</b>：2014-11-28下午1:50:20 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Controller
@Scope("prototype")
@RequestMapping("/index/")
public class IndexControl extends MobileBaseControl {


	/**
	 * 首页
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("home")
	public void home() throws Exception {
		List<ProjectModel> pjList =	projectService.getModelList(FrameUtil.newHashMap("sort"," pj_order desc ","is_spread",1,"is_used",1), DbWREnums.READ);

		List<Long> pjIdList = FrameUtil.getBeanFieldValues(pjList, "project_id");
		List<ResourceModel>  resourceModelList=null;

		if(pjIdList !=null){
			resourceModelList = resourceService.getModelList(FrameUtil.newHashMap("ref_type",2,"idList",pjIdList),DbWREnums.READ);
		}

		//推广项目
		List<Map<String, Object>> prjList = new ArrayList<Map<String, Object>>();
		for (ProjectModel projectModel:pjList){

			//设置项目的图片
			projectModel.setProject_pic(PicsResourceUtils.getPicsList(resourceModelList,projectModel.getProject_id()));

			// 构建项目返回信息
			Map<String, Object> projectResult = projectService.transformProjectToMap(projectModel);

			// 查询项目负责医生基本信息
			List<Map<String, Object>> doctorResult = doctorService.queryDoctor(FrameUtil.newHashMap("doctor_id", projectModel.getDoctor_id()));
			if(doctorResult.size() == 0){
				projectResult.put("doctorInfo", FrameUtil.newHashMap());
			} else {
				projectResult.put("doctorInfo", doctorResult.get(0));
			}

			prjList.add(projectResult);
		}


		//首页推广的机构
		List<OrganizationModel> orgInfoList  = organizationService.getModelList(FrameUtil.newHashMap("is_spread",1,"sort"," org_sort desc ") ,0,3,DbWREnums.READ);
		List<Long> orgIdList = FrameUtil.getBeanFieldValues(orgInfoList, "org_id");

		if(orgIdList !=null){
			resourceModelList = resourceService.getModelList(FrameUtil.newHashMap("ref_type",1,"idList",orgIdList),DbWREnums.READ);
		}
		List<Map<String, Object>>  orgList = new ArrayList<Map<String, Object>>();
		for (OrganizationModel organizationModel:orgInfoList){
			organizationModel.setOrg_pic(PicsResourceUtils.getPicsList(resourceModelList,organizationModel.getOrg_id()));
			orgList.add(ResultMapBuilder.build(organizationModel));
		}
		if(orgIdList.size()<3){
			int a = orgIdList.size();
			Map<String, Object> tempOrg = orgList.get(0);
			for (;a<3;a++){
				orgList.add(tempOrg);
			}
		}

		this.successData(FrameUtil.newHashMap("org_list",orgList,"prj_list",prjList));
	}


	@Resource
	private IProjectService projectService;
	@Resource
	private IDoctorService doctorService;
	@Resource
	private IResourceService resourceService;
	@Resource
	private IOrganizationService organizationService;


}
